<!--
 * @Author: jhw
 * @Date: 2025-08-05 23:58:16
 * @LastEditors: jhw
 * @LastEditTime: 2025-08-22 17:11:06
 * @Description:
-->
<template>
  <div key="id">
    <div class="flex items-center mb-10px">
      <div class="mr-10px">选项</div>
      <el-button size="small" :icon="Plus" circle @click="addOptionHandle" />
    </div>
    <div v-for="(item, index) in textArr" :key="index">
      <!-- 选项 -->
      <div class="title mt-10px mb-10px flex items-center">
        <span>选项{{ index + 1 }}</span>
        <el-button
          type="danger"
          class="ml-5 delete"
          size="small"
          :icon="Minus"
          circle
          @click="removeOption(index)"
        />
      </div>
      <!-- 是否上传图片 -->
      <div class="mb-5px">
        <div v-if="item.value" class="flex">
          <span class="title mr-5px">已上传图片</span>
          <el-link type="primary" @click="deletePic(index)">删除图片</el-link>
        </div>
        <span v-else class="title">未上传图片</span>
      </div>
      <!-- 修改图片标题 -->
      <el-input class="mb-5px" v-model="item.picTitle" placeholder="图片标题" />
      <!-- 修改图片描述 -->
      <el-input type="textarea" :rows="3" placeholder="图片描述" v-model="item.picDesc" />
    </div>
  </div>
</template>

<script setup lang="ts">
import type { GetLink, PicTitleDescStatusArr, UpdateStatus, VueComType } from '@/types';
import { Plus, Minus } from '@element-plus/icons-vue';
import { ElMessage } from 'element-plus';
import { inject, ref } from 'vue';

const props = defineProps<{
  status: PicTitleDescStatusArr;
  isShow: boolean;
  configKey: string;
  editCom: VueComType;
  id: string;
}>();

const textArr = ref(props.status);
const updateStatus = inject<UpdateStatus>('updateStatus');
const getLink = inject<GetLink>('getLink');

const addOptionHandle = () => {
  if (updateStatus) {
    updateStatus(props.configKey);
  } else {
    console.error('updateStatus is not found');
  }
};

const removeOption = (index: number) => {
  if (textArr.value.length === 2) return ElMessage.error('至少保留两个选项');
  if (updateStatus) {
    updateStatus(props.configKey, index);
  } else {
    console.error('updateStatus is not found');
  }
};

const deletePic = (index: number) => {
  if (getLink) {
    getLink({
      index,
      link: '',
    });
  } else {
    console.error('getLink is not found');
  }
};
</script>

<style scoped lang="scss">
.title {
  color: var(--font-color-light);
  font-size: var(--font-size-base);
}
.delete {
  transform: scale(0.8);
}
</style>
